package com.home.qh.webapp.util;

import java.text.DecimalFormat;

public class InterShipUtil {
    /**
     * Checks if is valid length.
     *
     * @param str the str
     * @param len the len
     * @return true, if is valid length 
     */
    public static boolean isValidLength(String str , int len){
        if(str != null && !"".equals(str)) {
            if(str.length() > len) {
            	return false;
            }
        }
        return true;
    }
    /**
     * Checks if is valid an length.
     *
     * @param str the str
     * @param len the len
     * @return true, if is valid an length
     * @throws AcddBusinessException the acdd business exception
     */
    public static boolean isValidAnLength(String str , int len){
        if(str != null && !"".equals(str)) {
            if(str.length() > len) {
            	return false;	
            }
        }
        return true;
    }
    public static boolean isValidLengthPattern(String str, int len, String pattern){
        if(str != null && !"".equals(str)) {
            if(str.length() > len) {
            	return false;
            }
            if(!str.matches(pattern))  {
            	return false;
            }
        }
        return true;    	
    }
    
    /**
     * Checks if is valid numeric length.
     *
     * @param val the val
     * @param number the number
     * @param decimal the decimal
     * @return true, if is valid numeric length
     * @throws AcddBusinessException the acdd business exception
     */
    public static boolean isValidNumericLength(Double val , int number, int decimal){
        if(val != null) {
        	StringBuffer decStr = new StringBuffer();
        	for(int i = 0; i < decimal ; i++) {
        		decStr.append("#");
        	}
        	
        	DecimalFormat format = new DecimalFormat("#" + (decimal > 0 ? "." + decStr : ""));
        	String numberStr = format.format(val);
        	
            if(numberStr.length() > (number + 1 + decimal)) {
            	return false;
            }
        }
        return true;
    }    
}
